<!--
* description: 质量活动维护
* author: wangjuan
* createdDate:2021-07-19
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      :key="oneTableKey"
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-table-cols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="false"
      :dynamic-is-show-more-btn="true"
    />

    <viewData
      ref="view"
      :key="viewPopupsKey"
      :dynamic-edit-row-data="viewRowData"
      :popups-visible.sync="viewPopupsVisible"
      :popups-state="editPopupsState"
      @closeviewData="closeviewData"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import Vue from 'vue'
import viewData from './viewData'
import api from '@/api/se/serviceActivities'

export default {
  name: 'QualityAM',

  components: {
    OneTableTemplate,
    viewData
  },

  // 组件混入对象：{data[listQuery] methods[queryTable]}
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, 'belowOrd-Edit'], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      viewPopupsKey: '',
      viewRowData: {},
      viewPopupsVisible: false,

      // 是否使用刷新Key的方式刷新弹窗
      resetDialogKey: false,

      // 网格查询API配置对象
      apiConfig: api.queryQualityActivityDlr,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: '活动类别：',
              codeField: 'category',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'category,categoryName',
              options: [
                { category: '1', categoryName: '召回' },
                { category: '2', categoryName: '市场质量活动' }
              ],
              type: 'dropdownList',
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '类型：',
              codeField: 'type',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'type,typeName',
              options: [
                { type: '1', typeName: '主动活动' },
                { type: '2', typeName: '被动活动' }
              ],
              type: 'dropdownList',
              isMust: true // 收起状态
            },
            {
              compKey: 'compKey4',
              labelName: '车系：',
              codeField: 'carSeriesCode',
              component: () => import('@/components/org/commonSelect'),
              customObj: {
                api: api.queryCarSeries,
                selectType: '1',
                defOptionFields: ['carSeriesCode', 'carSeriesCn']
              },
              type: 'dropdownList',
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '活动起止日期：',
              codeField: 'enableDate,closeDate',
              component: () => import('@/components/org/datePicker/datePicker'),
              dateType: 'daterange',
              isMust: true
            },
            {
              compKey: 'compKey6',
              labelName: '活动状态：',
              codeField: 'status',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'status,statusName',
              options: [
                { status: '2', statusName: '已发布' },
                { status: '3', statusName: '已关闭' }
              ],
              type: 'dropdownList',
              isMust: false // 收起状态
            },
            {
              compKey: 'compKey7',
              labelName: '活动编码：',
              codeField: 'activityCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: false // 收起状态
            },
            {
              compKey: 'compKey3',
              labelName: '活动主题：',
              codeField: 'theme',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: false
            }
          ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'releaseId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'activityId',
              label: '活动ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'activityCode',
              label: '活动编码',
              width: 140,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey2',
                  codeField: 'activityCode',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => {
                    this.viewData(index)
                  }
                }
              ]
            },
            {
              prop: 'theme',
              label: '活动主题'
            },
            {
              prop: 'carSeriesCn',
              label: '车系'
            },
            {
              prop: 'categoryName',
              label: '活动类别'
            },
            {
              prop: 'typeName',
              label: '类型'
            },
            {
              prop: 'notifyName',
              label: '通知方式'
            },
            {
              prop: 'statusName',
              label: '活动状态'
            },
            {
              prop: 'carAmount',
              label: '车辆数量'
            },
            {
              prop: 'amount',
              label: '金额'
            },
            {
              prop: 'enableDate',
              label: '活动开始日期'
            },
            {
              prop: 'closeDate',
              label: '活动截止日期'
            }
            // {
            //   prop: 'createdDate',
            //   label: '创建日期'
            // }
          ],

      formField: this.$utils.getFormField(this, {
        category: '',
        categoryName: '',
        type: '',
        typeName: '',
        carSeriesCode: '',
        carSeriesCn: '',
        enableDate: '',
        closeDate: '',
        status: '',
        statusName: '',
        activityCode: '',
        theme: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    }
  },

  mounted() {
    // 列表查询
    this.queryTable()
  },

  methods: {

    // 详情
    viewData(index) {
      this.viewPopupsVisible = true
      this.editPopupsState = 'edit'
      this.viewRowData = this.$refs['multipleTable'].list[index]
    },

    // // 关闭资料查看弹窗
    closeviewData() {
      this.viewPopupsVisible = false
      this.viewPopupsKey = this.$utils.generateId()
    }
  }
}
</script>
